[% setvar title Replace %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Replace =~, !~, m//, s///, and tr// with match(), subst(), and trade()</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 27 Aug 2000
  Last Modified: 15 Sep 2000
  Mailing List: <a href='mailto:perl6-language-regex@perl.org'>perl6-language-regex@perl.org</a>
  Number: 164
  Version: 3
  Status: Frozen
  Requires: RFC 170</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Several people (including Larry) have expressed a desire to get rid of
<code>=~</code> and <code>!~</code>. This RFC proposes a way to replace <code>m//</code>, <code>s///</code>,
and <code>tr///</code> with three new builtins, <code>match</code>, <code>subst</code>, and <code>trade</code>.
It also proposes a way to allow a full backwards-compatible syntax.</p>
<a name='NOTES ON FREEZE'></a><h1>NOTES ON FREEZE</h1>
<p>There appeared to be two types of people regarding this RFC: Those that
absolutely loved it, and those that hated it. I received many personal
emails saying what a good idea this was, but there was also considerable
dissention on the list about it. Most people agreed that the ability to
overload regex functions and also use them in common applications, such
as:</p>
<pre>   print subst /$old/$new/g, @input;</pre>
<p>Was a distinct benefit. The main uneasiness, which I agree with
completely, was not having the ability to do stuff like this:</p>
<pre>   next if /^$/ || /^#/;         # that's Perl, allright</pre>
<p>However, the RFC in its current format <i>does</i> provide for the ability
to do this still through its 100% backwards compatible syntax, which I
think actually satisfied many of the detractors.</p>
<p>Personally, the only way I would accept this proposal myself is if RFC
170 was also accepted, since that gives us a means for a backwards
compatible syntax. RFC 170 also has the nice side effect of extending
<code>=~</code> to other operations as well. My feeling in the end is that the
combination of this RFC and RFC 170 gives us the &quot;best of both worlds&quot;:</p>
<pre>   1. Prototypeable regex functions that can easily work on lists and
      be chained together, just like grep, map, split, and so forth.

   2. 100% backwards compatible, allowing the use of =~ for situations
      where it makes sense, but not where it doesn't.</pre>
<p>I honestly feel that the <i>combination</i> of this RFC and RFC 170 gives us
all the niceties of current Perl syntax with all the benefits of true
functions, and is a win as such.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Overview'></a><h2>Overview</h2>
<p>Everyone knows how <code>=~</code> and <code>!~</code> work. Several proposals, such as RFCs
135 and 138, attempt to fix some stuff with the current pattern-matching
syntax. Most proposals center around minor modifications to <code>m//</code> and
<code>s///</code>.</p>
<p>This RFC proposes that <code>m//</code>, <code>s///</code>, and <code>tr///</code> be dropped from the
language, and instead be replaced with new <code>match</code>, <code>subst</code>, and
<code>trade</code> builtins, with the following syntaxes:</p>
<pre>   $res [, $res] = match /pat/flags, $str [, $str];
   $res [, $res] = subst /pat/new/flags, $str [, $str];
   $res [, $res] = trade /pat/new/flags, $str [, $str];</pre>
<p>These subs are designed to mirror the format of <code>split</code>, making them
more consistent. Unlike the current forms, these return the modified
string, leaving the input <code>$str</code> alone.</p>
<p>Note that <code>replace</code> is likely a better name for <code>subst</code> since it
connotes the action better and is not so close to <code>substr</code>. Short on
time :-(, though, I won't have the chance to change all the examples.</p>
<p>Context modifies the return values just as Perl 5 context does, with
some extensions:</p>
<pre>   1. If called in a void context, they act on and modify C&lt;$_&gt;,
      consistent with current behavior.

   2. If called in a scalar context, C&lt;match&gt; returns the number
      of matches (like now), and the rest return the first (or
      only) string.

   3. If called in a list context, a list of the modified strings
      will be returned.

   4. If called in a numeric context, they all return the number
      of substitutions made.</pre>
<p>Extra arguments can be dropped, consistent with <code>split</code> and many other
builtins:</p>
<pre>   match;                  # all defaults (pattern is /\w+/)
   match /pat/;            # match $_
   match /pat/, $str;      # match $str
   match /pat/, @strs;     # match any of @strs

   subst;                  # strip leading/trailing whitespace
   subst /pat/new/;        # sub on $_
   subst /pat/new/, $str;  # sub on $str
   subst /pat/new/, @strs; # return array of modified strings

   trade;                  # nothing
   trade /pat/new/;        # tr on $_
   trade /pat/new/, $str;  # tr on $str
   trade /pat/new/, @str;  # return array of modified strings</pre>
<p>These new builtins eliminate the need for <code>=~</code> and <code>!~</code> altogether,
since they are functions just like <code>split</code>, <code>join</code>, <code>splice</code>, and so
on. There are also shortcut forms, see below.</p>
<p>Sometimes examples are easiest, so here are some examples of the new
syntax:</p>
<pre>   Perl 5                           Perl 6
   -------------------------------- ----------------------------------
   if ( /\w+/ ) { }                 if ( match ) { }
   die &quot;Bad!&quot; if ( $_ !~ /\w+/ );   die &quot;Bad!&quot; if ( ! match ); 
   ($res) = m#^(.*)$#g;             ($res) = match #^(.*)$#g;

   # These are longer, but you can still use the backwards
   # compatible syntax where it makes sense, like here

   next if /\s+/ || /\w+/;          next if match /\s+/ or match /\w+/;
   next if ($str =~ /\s+/) ||       next if match /\s+/, $str or 
           ($str =~ /\w+/)                  match /\w+/, $str;
   next unless $str =~ /^N/;        next unless match /^N/, $str;
   
   $str =~ s/\w+/$bob/gi;           $str = subst /\w+/$bob/gi, $str;
   s/\w+/this/;                     subst /\w+/this/;             

   tr/a-z/Z-A/;                     trade /a-z/Z-A/;
   $new =~ tr/a/b/;                 $new = trade /a/b/, $new;


   # Some become easier and more consistent...

   ($str = $_) =~ s/\d+/&amp;func/ge;   $str = subst /\d+/&amp;func/ge;
   ($new = $old) =~ tr/a/z/;        $new = trade /a/z/, $old;


   # And these are pretty cool...   

   foreach (@old) {                 @new = subst /hello/X/gi, @old;
      s/hello/X/gi;
      push @new, $_;
   }

   foreach (@str) {                 @new = trade /a-z/A-Z/, @str;
      tr/a-z/A-Z/;
      push @new, $_;
   }

   $gotit = 1;                      print &quot;Got it&quot; if match /\w+/, @str;
   foreach (@str) {
      undef $gotis unless /\w+/;
   }
   print &quot;Got it&quot; if $gotit;</pre>
<p>This gives us a cleaner, more consistent syntax. In addition, it makes
several things easier, is more easily extensible:</p>
<pre>   &amp;callsomesub(subst(/old/new/gi, $mystr));
   $str = subst /old/new/i, $r-&gt;getsomeval;</pre>
<p>and is easier to read English-wise. However, it requires too much
typing. For that reason, we include the shortcut form as well:</p>
<a name='Shortcut Form'></a><h2>Shortcut Form</h2>
<p>RFC 139 describes a way that the <code>//</code> syntax can be expanded to any
function. So, to gain backwards compatibility, we simply allow this
syntax along with the shortcut function names <code>s</code>, <code>m</code>, and <code>tr</code> [1]:</p>
<pre>   Shortcut Form                    Builtin
   -------------------------------- ----------------------------------
   s/\w+/W/g;                       subst /\w+/W/g;
   /\w+/;                           match /\w+/;
   tr/ae/io/;                       trade /ae/io/;

   $new = s/\s+/X/, $old;           $new = subst /\s+/X/, $old;
   $num = m/\w+/, $str;             $num = match /\w+/, $str;
   $new = tr/a-z/z-a/, $str;        $new = trade /a-z/z-a/, $str;</pre>
<p>Note <code>//</code> can still be used as a shortcut to <code>m//</code>. This is the form
most people will use, I would imagine. Starting to look like Perl 5...</p>
<a name='Use of =~ Syntax'></a><h2>Use of <code>=~</code> Syntax</h2>
<p>RFC 170 shows how <code>=~</code> can be used as a more generic assignment
operator / rvalue duplicator. With this ability, we can now write all
our Perl 5 regex syntaxes still, even though they're actually new Perl 6
builtins:</p>
<pre>   Shortcut Form + C&lt;=~&gt;            Builtin
   -------------------------------- ----------------------------------
   $str =~ s/\w+/W/g;               $str = subst /\w+/W/g, $str;
   $str =~ tr/a-z/z-a/;             $str = trade /a-z/z-a/, $str;
   $str =~ /\w+/;                   match /\w+/, $str;       # See [2]
   ($match) = /^(.*)$/g;            ($match) = match /^(.*)$/g;

   # Can't do these in Perl 5
   @str =~ s/$foo/$bar/gi;          @str = subst /$foo/$bar/gi, @str;
   @str =~ tr/a-z/A-Z/;             @str = trade /a-z/A-Z/, @str; 
   @str =~ m/^Pass:/;               match /^Pass:/, @str;</pre>
<p>So, why all the bother if it looks just like Perl 5? Well, these last
two sections are based on more general mechanisms for Perl 6. That is,
allowing the generalization of <code>=~</code> and the <code>//</code> syntax allows us to
write these expressions in a way that is backwards compatible. However,
there is no explicit relationship between the Perl 5 backwards-compat
syntax and the new Perl 6 syntax, even though there appears to be.</p>
<p>In fact, the mechanism covered in RFC 170 allows us to write stuff like:</p>
<pre>   $str =~ quotemeta;          # $str = quotemeta($str);
   @a =~ sort { $a &lt;=&gt; $b };   # @a = sort { $a &lt;=&gt; $b } @a;</pre>
<p>So you can see how this general purpose mechanism allows us backwards
compatibility.</p>
<p>Finally, note how we have a good amount of flexible, parallel syntax
because of this:</p>
<pre>   $str =~ s/$foo/$bar/gi;     # just a general shorcut
   $new = s/$bar/$baz/g, $str; # more consistent when $new != $str</pre>
<a name='Concerns'></a><h2>Concerns</h2>
<p>Because of the fact that this proposal has the ability to be 100%
backwards compatible, it doesn't strike me as problematic anymore.
However, it should still be a conscious decision to change pattern
matching at all. I have no interest in breaking Perl regex's. At all.
None.</p>
<p>Still, I have received many personal emails in favor of this idea. So,
if implemented correctly, I think it could be a benefit for Perl 6.</p>
<p>Note that <code>trade</code> was chosen because &quot;transliterate&quot; is way too long
and &quot;trans&quot; looks to be taken by transactional variables. And <code>trade</code>
seems to connote the action pretty well still.</p>
<p>Finally, many have expressed the fear that <code>subst</code> is much too close to
<code>substr</code>, and I agree. The best alternative suggested was <code>replace</code>,
which I like. The only problem is that the shortcut version would be
<code>s///</code> instead of <code>r///</code> (to maintain backwards compatibility), but
this is a detail that can be worked out later, if this RFC is adopted.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Major changes to <code>=~</code>, <code>!~</code>, <code>m//</code>, <code>s///</code>, and <code>tr///</code>. Additions
of three new functions, <code>match</code>, <code>trade</code>, and <code>substr</code> / <code>replace</code>.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>There are no longer any syntax changes as of v2. No migration path is
required, assuming this is implemented carefully.</p>
<a name='NOTES'></a><h1>NOTES</h1>
<p>[1] If most people are going to continue using the shortcut form and
names, it might be wise just to make the functions be named <code>m</code>, <code>s</code>,
and <code>tr</code>, even though these are silly function names.</p>
<p>[2] <code>match</code> is a bit of a special case, just like <code>m//</code> is when
compared to <code>s///</code> and <code>tr///</code>. The support of <code>!~</code> and <code>m//</code> will
have to be explored some more, but I'll leave that for subsequent
discussions.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>This is a synthesis of several ideas from myself, MJD, Ed Mills, Steve
Fink, and Tom C</p>
<p>RFC 138: Eliminate =~ operator.</p>
<p>RFC 139: Allow Calling Any Function With A Syntax Like s///</p>
<p>RFC 170: Generalize =~ to a special-purpose assignment operator</p>
</div>
